package space.jamestang.simpleci.schema

import org.ktorm.entity.Entity
import space.jamestang.simpleci.config.ConfigChecker
import space.jamestang.simpleci.globalLogger
import space.jamestang.simpleci.runner.Executor
import java.io.File

interface Project: Entity<Project> {

    companion object : Entity.Factory<Project>()

    var id: Int?
    var name: String
    var description: String
    var gitUrl: String
    var branch: String
    var buildCommand: List<Command>
    var environment: List<Environment>


    fun initializeProject(project: Project): Boolean{

        globalLogger.info("${ConfigChecker.config.reposDir!!}/${project.name}")
        val reposDir = File("${ConfigChecker.config.reposDir!!}/${project.name}")

        var result = false
        result = when(reposDir.exists() && reposDir.isDirectory){

            true -> {
                Executor(reposDir.absolutePath, null, "git", "pull").execute() == 0
            }

            false -> {
                Executor(reposDir.parent, null, "git", "clone", project.gitUrl).execute() == 0
            }
        }

        return result
    }
}